<?php
/**
 * List of Mission Partner that are not candidate
 * 
 * 
 */

use Fox\Utilities;
use Doctrine\DBAL\Connection;
use Fox\Dao\SqlFragment;
use Fox\Html\Screen;
use Fox\Generator;
use Fox\Behaviors;
use Fox\Html\Button;
use Fox\Html\Header\Content;
use Hive\Mission\Partner;
use Hive\Mission;

$xview = Xview::instance();
/* @var $mission Hive\Mission */
$mission = $xview->component('missions', 'object');

// Customizing
$customizing = Hive\Mission\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'missions','act'=>'partners'));

$data = $mission->getData();
// Load All Others Contacts
$filters[] = new SqlFragment('role NOT IN (?)',array(array(Mission\Partner::ROLE_CANDIDATE,
					Mission\Partner::ROLE_RESPONSIBLE,Mission\Partner::ROLE_ACCOUNT)),array(Connection::PARAM_STR_ARRAY));
$data['gContacts'] = $mission->contactsInfos($filters);

// Load Customer Name
try {
	$contact = Fox\Dao::load(Hive\Ressource::className(),$data['customerid']);
	$data['customer'] = $contact->name();
}
catch (Fox\Object\Exception\NotExist $e) {
	// do nothing
}

// Screen - 
$title = new Content('partners',Msg(Hive::BUNDLE,Hive::MENU_CONTACTS));
// Display Contact Screen
$screen = new Screen('sContacts','update',null,true);
$block = $xview->component('missions', 'header');
$block->id('createMission');
$screen->add($block);
// Contact has an array
$blContacts = $xview->component('missions', 'contacts');
$blContacts->item('gContacts')->footer()->add(new Fox\Html\Button(array('add','addContact')))
				->behaviors(Behaviors::action(
				Generator::url('missions','contacts','add',
				array('missionid' => $mission->getId())),true
));
$screen->add($blContacts);
$screen->initialize($data);
if($screen->submitted())
{
	$inputs = $screen->get();
	$inputs['grouping'] = !empty($inputs['grouping']) ? $inputs['grouping'] :
								 $block->item('grouping')->firstAvailableValue();
	if(!$screen->errors()) {
		// Check if the contact already exist ?
		if(!empty($inputs['customerid']))
		{
			
		} else {
			// Contact Does not Exist 
			$url = Generator::url('ressource','add',null,array('name1' => $inputs['customer']));
			Fox\Message::Error(Msg(Hive::BUNDLE,
								Hive::CONTACT_DOES_NOT_EXIST_YET,
								$inputs['customer'],
								$url));
		}		
	}
	if(!$screen->errors())
	{
		try {
			
			$missionid = $mission->getId();
			$customerid = $mission->customerid;
			$companid = $mission->companid;
		
			// Check if the school already exist
			if(!empty($inputs['companyName']))
			{
				$company = Hive\Company::lookup($inputs['companyName']);
				if(!$company)
				{
					$company = new Hive\Company();
					$company->setFields(array('company_name' => $inputs['companyName']));
					$company->create();
				}
				$inputs['companid'] = $company->getId();
			}
			
			$mission->setFields($inputs);
			if($companid != $inputs['companid']) {
				// Adding Mission Partners - Company
				$companyPartner = $mission->findPartners($missionid.':'.Partner::ROLE_ACCOUNT.':'.$companid);
				if($companyPartner->exists()) {
					$mission->deleteFromPartners($companyPartner);
				}
				$newCompanyPartner = new Partner();
				$newCompanyPartner->setFields(array('partnerid' => $inputs['companid'],'role' => Partner::ROLE_ACCOUNT));
				$mission->addToPartners($newCompanyPartner);
			}
			if($customerid != $inputs['customerid']) {
				// Adding Mission Partners - Company
				if(!empty($customerid)) {
					$soldtoPartner = $mission->findPartners($missionid.':'.Partner::ROLE_SOLD_TO.':'.$customerid);
					$mission->deleteFromPartners($soldtoPartner);
				}
				$newSoldtoPartner = new Partner();
				$newSoldtoPartner->setFields(array('partnerid' => $inputs['customerid'],
										'role' => Partner::ROLE_SOLD_TO));
				$mission->addToPartners($newSoldtoPartner);
			}
			
			// 

			// Update
			$mission->prepareUpdate(Mission::CHANGE_DEFAULT);
			if($mission->update()) {
				Fox\Message::Success(Msg(Hive::BUNDLE,
										Hive::MISSION_SUCCESSFULLY_UPDATED,
										$inputs['reference']));
			} else {
				Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE,$inputs['reference']));
			}
		}
		catch(Fox\Object\Exception\Save $e) {
			var_dump($e);
			Fox\Message::Error($e->getMessage());
		}
	}
	echo $xview->messages();
	exit;
}
echo $title->render();
echo '<hr>';
echo $screen->display();